﻿#region
//Copyright (c) 2016-2022 北京网非科技有限公司(wfdot.com)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SumiaAccountBook.DataAccess
{
    public static class DaCommon<T> where T : class,Entity.ICommon
    { 
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<T> GetList(int pageIndex, int pageSize,ref int total)
        {
            List<Entity.KeyValue> result = new List<Entity.KeyValue>();
            using (CommonContext<T> db = new CommonContext<T>())
            {
                total = db.All.Count();                
                return db.All.OrderByDescending(d => d.Key).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();                
            }            
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public static List<T> GetAll()
        {
            List<Entity.KeyValue> result = new List<Entity.KeyValue>();
            using (CommonContext<T> db = new CommonContext<T>())
            {
                return db.All.OrderByDescending(d => d.Key).ToList();
            }
        }

        /// <summary>
        /// 更新配置
        /// </summary>
        /// <param name="entity"></param>
        public static void Update(T entity)
        {
            using(CommonContext<T> db=new CommonContext<T>())
            {
                db.Entry<T>(entity).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public static void Delete(int Id)
        {
            using (CommonContext<T> db = new CommonContext<T>())
            {
                var deleted = db.All.Where(d => d.Key == Id);
                db.All.RemoveRange(deleted);
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 如果主键不是自增的，出异常
        /// </summary>
        /// <param name="entity"></param>
        public static void Add(T entity)
        {
            using (CommonContext<T> db = new CommonContext<T>())
            {
                db.All.Add(entity);
                db.SaveChanges();
            }
        }       
    }
}
